#include <WinSock2.h>

#include "Box2D\Box2D.h"

#include "hge\hge.h"
#include "hge\hgesprite.h"
#include "hge\hgefont.h"
#include "hge\hgeresource.h"

#include "aerial\Game\Helper\BattleHelper.h"
#include "aerial\Game\GameObject\GameObject.h"
#include "aerial\Game\Helper\AirContactListener.h"

#include "testBox2d\TestGameObject.h"

#include <iostream>

HGE* hge;
hgeFont* fnt;
hgeResourceManager hrm;
b2World* world = 0;

TestGameObject *a, *b;
AirContactListener acl;

using namespace std;

bool FrameFunc()
{
	static bool lstatus = false;
	float dt = hge->Timer_GetDelta();
	float ctime = hge->Timer_GetTime();
	
	if (hge->Input_GetKeyState(HGEK_W)) a->accUp();
	if (hge->Input_GetKeyState(HGEK_S)) a->accDown();
	if (hge->Input_GetKeyState(HGEK_A)) a->accLeft();
	if (hge->Input_GetKeyState(HGEK_D)) a->accRight();

	if (hge->Input_GetKeyState(HGEK_UP))		b->accUp();
	if (hge->Input_GetKeyState(HGEK_DOWN))	b->accDown();
	if (hge->Input_GetKeyState(HGEK_LEFT))	b->accLeft();
	if (hge->Input_GetKeyState(HGEK_RIGHT)) b->accRight();

	world->Step(1.f/60.f, 8, 3);

	a->CoordinateTransform();
	b->CoordinateTransform();

	acl.process(dt);


	return false;
}

bool RenderFunc()
{
	hge->Gfx_BeginScene();
	hge->Gfx_Clear(0);
	fnt->printf(5, 5, HGETEXT_LEFT, "dt: %f", hge->Timer_GetDelta());
	a->render();
	b->render();

	fnt->printf(5, 55, HGETEXT_LEFT, "hp_a: %d", (int)a->hp);
	fnt->printf(5, 105, HGETEXT_LEFT, "hp_b: %d", (int)b->hp);

	hge->Gfx_EndScene();
	return false;
}

int main()
{

	hge = hgeCreate(HGE_VERSION);

	hge->System_SetState(HGE_LOGFILE, "bulletTest.log");
	hge->System_SetState(HGE_FRAMEFUNC, FrameFunc);
	hge->System_SetState(HGE_RENDERFUNC, RenderFunc);
	hge->System_SetState(HGE_TITLE, "A.I.R barage");
	hge->System_SetState(HGE_WINDOWED, true);
	hge->System_SetState(HGE_SCREENWIDTH, 800);
	hge->System_SetState(HGE_SCREENHEIGHT, 600);
	hge->System_SetState(HGE_SCREENBPP, 32);
	hge->System_SetState(HGE_HIDEMOUSE, false);
	hge->System_SetState(HGE_SHOWSPLASH, false);
	hge->System_SetState(HGE_FPS, 60);

	if(hge->System_Initiate())
	{
		hrm.ChangeScript("aeria.res");
		hrm.Precache();

		fnt = hrm.GetFont("fnt.original");
		if (fnt) {
			b2Vec2 gravity(0.f, 0.f);
			world = new b2World(gravity);
			
			IObject::initialize(hge, &hrm, 0, 0);
			GameObject::SetWorld(world);
			BattleHelper::setRatio(50.f);
			BattleHelper::setMapHW(800.f, 600.f);
			world->SetContactListener(&acl);

			a = new TestGameObject(300.f, 400.f, 0xff0000ff, 0);
			b = new TestGameObject(300.f, 500.f, 0xff00ff00, 0);

			hge->System_Start();
		}
		hrm.Purge();
		delete world;
	}
	hge->System_Shutdown();
	hge->Release();
	return 0;
	
	return 0;
}